//
//  UIButton + Extension.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/22.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// 便利构造函数
    ///
    /// - parameter title:           标题
    /// - parameter titleColor:      标题颜色
    /// - parameter fontSize:        标题文字大小
    /// - parameter imageName:       图片名
    /// - parameter backgroundColor: 背景颜色
    ///
    /// - returns: 按钮
    convenience init(title: String, titleColor: UIColor, fontSize: CGFloat, imageName: String?, backgroundColor: UIColor) {
        
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(titleColor, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        self.backgroundColor = backgroundColor
        
        if let imgName = imageName {
            setImage(UIImage(named: imgName), forState: .Normal)
        }
        
        sizeToFit()
    }
    
    /// 便利构造函数
    ///
    /// - parameter imageName:     图片名称
    /// - parameter backImageName: 背景图片名称
    ///
    /// - returns: UIButton
    convenience init(imageName: String, backImageName: String? = nil) {
        self.init()
        
        setImage(UIImage(named: imageName), forState: .Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        
        if let backImgName = backImageName {
            setBackgroundImage(UIImage(named: backImgName), forState: .Normal)
            setBackgroundImage(UIImage(named: backImgName + "_highlighted"), forState: .Highlighted)
        }
        
        sizeToFit()
    }
    
    /// 便利构造函数
    ///
    /// - parameter title:         标题
    /// - parameter backColor:         按钮颜色
    /// - parameter fontSize:      标题文字大小
    /// - parameter backImageName: 背景图片名称
    ///
    /// - returns: UIButton
    convenience init(title: String? = nil, backColor: UIColor, fontSize: CGFloat = 17, backImageName:String? = nil) {
        self.init()
        
        setTitleColor(backColor, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        guard let str = title else {
            return
        }
        
        guard let imgName = backImageName else {
            return
        }
        
        setTitle(str, forState: .Normal)
        setBackgroundImage(UIImage(named: imgName), forState: .Normal)
        setBackgroundImage(UIImage(named: imgName + "_highlighted"), forState: .Highlighted)
        
        sizeToFit()
    }
    
    ///  便利构造函数
    ///
    /// - parameter title:     title description
    /// - parameter color:     color description
    /// - parameter fontSize:  fontSize description
    /// - parameter imageName: imageName description
    ///
    /// - returns: UIButton
    convenience init(title: String? = nil, color: UIColor,fontSize: CGFloat = 17, imageName:String? = nil) {
        self.init()
        
        setTitleColor(color, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        guard let str = title else {
            return
        }
        
        guard let imgName = imageName else {
            return
        }
        
        setTitle(str, forState: .Normal)
        setImage(UIImage(named: imgName), forState: .Normal)
        setImage(UIImage(named: imgName + "_highlighted"), forState: .Highlighted)
        
        
        sizeToFit()
    }
    

    
    
}
